// pages/Alliance/Alliance.js
// pages/find_owner_pet/find_owner_pet.js
var Url = "http://localhost:8085/";
//引入请求代码
var call = require("../../utils/ajax.js");
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        URL: "",
        className: "",
        region: "",
        loadingMore: true,
        noMore: false,
        loadingFailed: false,
        loading: false,
        //初始页
        pageNo: 1,
        // token
        tokenID: "",
        // 联盟信息
        union: {},
        //联盟唯一id
        organization_id: "1177507970335203328",
        //类别
        category: "",
        //地区
        region: "",
        // 数据源
        organization: [],
        openID: "",
        selectArray: [{
                "id": "1",
                "text": "不限"
            },
            {
                "id": "0",
                "text": "救助机构"
            }, {
                "id": "1",
                "text": "官方机构"
            }
        ],
        adress: [{
                "id": "1",
                "text": "不限"
            },
            {
                "id": "2",
                "text": "福州"
            }, {
                "id": "3",
                "text": "厦门"
            }, {
                "id": "4",
                "text": "漳州"
            }, {
                "id": "5",
                "text": "龙岩"
            }, {
                "id": "6",
                "text": "宁德"
            }, {
                "id": "7",
                "text": "平潭"
            }, {
                "id": "8",
                "text": "泉州"
            }, {
                "id": "9",
                "text": "三明"
            }, {
                "id": "10",
                "text": "莆田"
            }, {
                "id": "11",
                "text": "南平"
            }
        ],
        adress1: "测试页面传值",
        phone: "123123",
        weixin: "测试微信传值",
        display: "none",
        display_head1: "block",
        display_head2: "none"
    },
    // properties: {
    //   adress: {
    //     type: String,
    //     value: ""
    //   }
    // },
    //联系他点击事件
    show: function(e) {
        var that = this;
        console.log(e.target.dataset.index);
        var index = e.target.dataset.index;
        let adress_copy = that.data.organization[index].organization_big_area;
        let phone = that.data.organization[index].organization_contact;
        let weixin = that.data.organization[index].organization_wechat;
        wx.showActionSheet({
                itemList: ['复制联系人：' + phone, '复制地址：' + adress_copy, '复制微信：' + weixin],
                success: function(res) {
                    let inde = res.tapIndex;
                    switch (inde) {
                        case 0:
                            wx.setClipboardData({
                                data: phone,
                                success: function(res) {
                                    wx.getClipboardData({
                                        // 这个api是把拿到的数据放到电脑系统中的
                                        success: function(res) {
                                            console.log(res.data) // data
                                        }
                                    })
                                }
                            })
                            break;
                        case 1:
                            wx.setClipboardData({
                                data: adress_copy,
                                success: function(res) {
                                    wx.getClipboardData({
                                        // 这个api是把拿到的数据放到电脑系统中的
                                        success: function(res) {
                                            console.log(res.data) // data
                                        }
                                    })
                                }
                            })
                            break;
                        case 2:
                            wx.setClipboardData({
                                data: weixin,
                                success: function(res) {
                                    wx.getClipboardData({
                                        // 这个api是把拿到的数据放到电脑系统中的
                                        success: function(res) {
                                            console.log(res.data) // data
                                        }
                                    })
                                }
                            })
                            break;
                    }
                    console.log(res.tapIndex)
                },
                fail: function(res) {
                    console.log(res.errMsg)
                }
            })

    },
    changed: function(e) {
        console.log(e);
        this.setData({
            display: e.detail.display
        })
    },
    // 获取下拉菜单所选中的值
    getDate: function(e) {
        console.log("类别--" + e.detail.text)
        var that = this;
        that.setData({
            category: e.detail.text
        });
        this.onLoad_list();
    },
    getDate1: function(e) {
        console.log("类别--" + e.detail.text)
        var that = this;
        that.setData({
            region: e.detail.text
        });
        this.onLoad_list();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
                this.setData({
                        URL:app.globalData.URL
                    })
        let dd = wx.getStorageSync("tokenID");
        this.setData({
                tokenID: dd
            })
        this.onLoad_list();
        //加载联盟信息
        this.union();
    },
    //加载联盟信息
    union() {
        var that = this;
        console.log("=====token=====" + this.data.tokenID);
        console.log("加载联盟信息");
        let userState = "";
        userState = wx.getStorageSync('userState');
        console.log(userState);
        that.setData({
            union: userState
        })
    },
    // 加载联盟列表
    onLoad_list() {
        var that = this;
        console.log("==========" + that.data.category);
        console.log("加载联盟列表");
        let category = that.data.category;
        let region = that.data.region;
        let url = "Organization/getOrganization/1/10?";
        if (category != undefined && category != "" && category != "不限") {
            url = url + "className=" + category + "&"
        }
        if (region != undefined && region != "" && region != "不限") {
            url = url + "region=" + region + "&"
        }
        // let url = 'Organization/getOrganization/1/10?className=' + that.data.category + '&region=' + that.data.region;
        call.getData1(url, function(res) {
            that.setData({
                organization: res.date.list,
                pageNo: 1
            })
        }, function() {

        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
let that = this;
        if (app.globalData.userLogin) {
            this.setData({
                display_head1: "none"
            })
            this.setData({
                display_head2: "block"
            })
            let userState = "";
            userState = wx.getStorageSync('userState');
            console.log(userState);
            that.setData({
                union: userState
            })
        } 
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        console.log("拉了我")
            // console.log("拉到位");
        console.log("联盟")
        if (!this.data.loading && !this.data.noMore) {
            this.setData({
                loading: true,
                pageNo: this.data.pageNo + 1
            });
            this.getData(true);
        }

    },
    //请求数据
    getData(isPage) {
        let admin = this;
        // console.log("需要拼接数据：", Network.pageData);
        let params = {
            pageNum: admin.data.pageNo,
            pageSize: 2
        };
        //请求
        admin.setData({
            loadingMore: false
        });
        let category = this.data.category;
        let region = this.data.region;
        let url = "Organization/getOrganization/" + params.pageNum + "/" + params.pageSize + "?";
        if (category != undefined && category != "" && category != "不限") {
            url = url + "className=" + category + "&"
        }
        if (region != undefined && region != "" && region != "不限") {
            url = url + "region=" + region + "&"
        }
        console.log(url)
        call.getData1(url, function(res) {
            console.log(res)
            let jj = res.date.list;
            console.log(jj);
            console.log(res.date.pageNum)
            admin.setData({
                loading: false,
                loadingMore1: false
            })
            if (res.code == 1000) {
                if (isPage && res.date.pageNum != 1) {
                    //下一页的数据拼接在原有数据后面
                    if (res.date.pageNum != 1 && admin.data.pageNo <= res.date.pages) {
                        admin.setData({
                            organization: admin.data.organization.concat(jj)
                        })
                    }

                } else {
                    //第一页数据直接赋值
                    admin.setData({
                        organization: jj
                    })
                }
                //如果返回的数据为空，那么就没有下一页了
                if (res.date.list.length == 0) {
                    admin.setData({
                        noMore: true
                    })
                }
                //如果是最后一页，那么就没有下一页了
                if (res.date.pageNum == res.date.pages) {
                    admin.setData({
                        noMore: true
                    })
                }
            } else {
                //返回失败
                admin.setData({
                    loadingFailed: true
                })
            }
        }, function(e) {
            console.log(e)
        })
    },
    // 获取滚动条当前位置
    onPageScroll: function(e) {
        // console.log(e)
        if (e.scrollTop > 3) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    //回到顶部
    goTop: function(e) { // 一键回到顶部
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})